package edu.njust.cma.mapper;

import edu.njust.cma.pojo.Task;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface TaskMapper {

    @Select("select * from tasks where small_project_id=#{smallProjectId}")
    List<Task> getTask(Integer smallProjectId);

    @Select("select * from tasks where small_project_id=#{smallProjectId}")
    List<Task> getTaskBySmallProjectId(int smallProjectId);

    @Select("select * from tasks where id=#{taskId}")
    Task getTaskById(Integer taskId);
    @Insert("insert into tasks(small_project_id,standard_method_id,name,creation_time)" +
            " values(#{smallProjectId},#{standardMethodId},#{name},#{creationTime})")
    void addTask(int smallProjectId, int standardMethodId, String name, String creationTime);


    @Select("select count(*) from tasks where small_project_id=#{smallProjectId}")
    int getCountTaskBySmallProjectId(Integer smallProjectId);

    @Select("select count(*) from tasks where small_project_id=#{smallProjectId} and status = 1")
    int getCountTaskCompleteBySmallProjectId(Integer smallProjectId);

    @Update("update tasks set status=#{status} where id=#{taskId}")
    void updateStatus(int taskId, int status);

    @Select("select project_id from small_project where id=(select small_project_id from tasks where id=#{taskId})")
    Integer getProjectIdByTaskId(Integer taskId);

    @Select("select status from tasks where id in " +
            "(select id from tasks where small_project_id in" +
            " (select id from small_project where project_id=#{projectId}))")
    List<Integer> getStatusByProjectId(Integer projectId);


    @Select("select count(*) from tasks where small_project_id in " +
            "(select id from small_project where project_id = #{projectId})")
    Integer getCountTaskByProjectId(int projectId);

    @Select("select count(*) from tasks where small_project_id in " +
            "(select id from small_project where project_id = #{projectId}) and status = 2")
    Integer getCountTaskCompleteByProjectId(int projectId);
}
